package cn.hallele.leinsteining.ms.support.starter.components.controller.aspect;

import cn.hallele.leinsteining.mc.infrastructure.metadata.constants.MsgCodeConst;
import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Metric;
import cn.hallele.leinsteining.mc.infrastructure.metadata.io.Response;
import cn.hallele.leinsteining.ms.infrastructure.util.network.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

/**
 * BaseExceptionAspect
 *
 * @author anle5
 * @since 2024/7/23 23:25
 */
@Slf4j
public class BaseExceptionAspect {
    @Autowired
    protected MessageSource i18n;

    /**
     * 构建响应
     *
     * @param msgCode 响应码
     * @param msgDesc 响应描述
     * @return 响应
     */
    protected Response<Object> buildResponse(String msgCode, String msgDesc) {
        return Response.builder()
                .metric(Metric.builder()
                        .success(MsgCodeConst.SUCCESS.equals(msgCode))
                        .msgCode(msgCode)
                        .msgDesc(msgDesc)
                        .ip(HttpUtil.getClientIp())
                        .accessDevice(HttpUtil.getClientInfo())
                        .build())
                .build();
    }

    /**
     * 获取异常信息
     *
     * @param e 异常
     * @return 异常信息
     */
    protected String getErrorMsg(Throwable e) {
        return StringUtils.isBlank(e.getMessage()) ? e.getCause().getMessage() : e.getMessage();
    }
}
